﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.AddIn;
using System.AddIn.Contract;
using System.AddIn.Hosting;
using System.AddIn.Pipeline;
using HostView;

namespace HostApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            load();
        }
        ICollection<AddInToken> addins;
        int a = 1, b = 1;

        private void load()
        {
            string root = Environment.CurrentDirectory;
            AddInStore.Rebuild(root);
            addins = AddInStore.FindAddIns(typeof(_HostView), root);
            int i = 0;
            foreach (AddInToken token in addins)
            {
                //_HostView ins = token.Activate<_HostView>(AddInSecurityLevel.Internet);
                Button btn = new Button();
                btn.Content = token.Name;
                btn.Click += new RoutedEventHandler(btn_Click);
                stack.Children.Add(btn);
            }
        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            foreach (AddInToken token in addins)
            {
                if (token.Name == btn.Content.ToString())
                {
                    _HostView ins = token.Activate<_HostView>(AddInSecurityLevel.Internet);
                    MessageBox.Show("a = " + a.ToString() + ", b = " + b.ToString() + ", result: " + ins.calculate(a, b).ToString());
                    break;
                }
            }
        }

        private void tbNumber1_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            try
            {
                if (tb == tbNumber1)
                    a = Convert.ToInt32(tb.Text);
                else
                    b = Convert.ToInt32(tb.Text);
            }
            catch
            {
                a = 1;
                b = 1;
            }
        }
    }
}
